﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;

namespace OtherDevice
{
    [Description("光源控制器-CST")]
    public class HY_Light_CST:LightCtrlBase
    {
        public HY_Light_CST(string strName) : base(strName)
        {
            controlFrom = new XNCLightCtrl();
        }
        public override int Get(int channel)
        {
            return 0;
        }
        public override bool Set(int channel, int value)
        {
            try
            {
                string strChannal = "";
                if (channel == 0)
                    strChannal = "A";
                else if (channel == 1)
                    strChannal = "B";
                else if (channel == 2)
                    strChannal = "C";
                else if (channel == 3)
                    strChannal = "D";
                else if (channel == 4)
                    strChannal = "E";
                else if (channel == 5)
                    strChannal = "F";
                else if (channel == 6)
                    strChannal = "G";
                else if (channel == 7)
                    strChannal = "H";
                string str_Value = $"{value/1000}";
                value = value % 1000;
                str_Value = $"{str_Value}{value / 100}";
                value = value % 100;
                str_Value = $"{str_Value}{value / 10}";
                value = value % 10;
                str_Value = $"{str_Value}{value}";
                string ReadCMD = $"S{strChannal}{str_Value}#";
                ReadCMD = ReadCMD.Trim();
                byte[] byteArray = System.Text.Encoding.ASCII.GetBytes(ReadCMD);
                int n_RetryTime = 0;
            Retry:
                SendData(byteArray);
                byte[] byteRe = new byte[2];
                int ret = ReceiveData(ref byteRe, 500);
                if (ret != 0)
                {
                    n_RetryTime++;
                    if (n_RetryTime < 3)
                        goto Retry;
                    else
                        return false;
                }
                else
                {
                    return true;
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show($"光源设置失败{ex}");
                return false;
            }
        }

    }
}